package service

import (
	"anban/models"
	"anban/utils"
	"fmt"
	"github.com/astaxie/beego/orm"
	"net/url"
	"strconv"
	"time"
)

// 获取新通知信息
func GetNewNoticeInfo(p map[string]interface{}) *models.NewNotice {
	o := orm.NewOrm()
	newNotice := &models.NewNotice{}
	qs := o.QueryTable("NewNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	qs.One(newNotice)
	return newNotice
}

// 添加单条新通知信息
func AddNewNotice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	newNotice := &models.NewNotice{}
	user := &models.User{}
	if _, ok := input["user_id"]; ok {
		user.Id = utils.Atoi64(input["user_id"][0])
		o.Read(user)
	}
	newNotice.User = user
	student := &models.Student{}
	if _, ok := input["student_id"]; ok {
		student.Id = utils.Atoi64(input["student_id"][0])
		o.Read(student)
	}
	newNotice.Student = student
	if _, ok := input["type"]; ok {
		newNotice.Type, _ = strconv.Atoi(input["type"][0])
	}
	return o.Insert(newNotice)
}

// 按班级添加新通知信息
func AddNewNotices(input url.Values) (int64, error) {
	o := orm.NewOrm()
	var newNotices []models.NewNotice
	user := &models.User{}
	if _, ok := input["user_id"]; ok {
		user.Id = utils.Atoi64(input["user_id"][0])
		o.Read(user)
	}
	p := map[string]interface{}{}
	p["classId"] = input["class_id"][0]
	_, studentList := GetStudentList(p)
	mType, _ := strconv.Atoi(input["type"][0])
	for _, student := range studentList {
		newNotice := models.NewNotice{}
		newNotice.User = user
		newNotice.Type = mType
		newNotice.Student = student
		newNotices = append(newNotices, newNotice)
	}
	return o.InsertMulti(1, newNotices)
}

// 修改新通知信息
func EditNewNotice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("NewNotice").Filter("id", input["id"][0]).Update(p)
}

// 获取新通知列表
func GetNewNoticeList(p map[string]interface{}) (int64, []*models.NewNotice) {
	var newNoticeList []*models.NewNotice
	o := orm.NewOrm()
	qs := o.QueryTable("NewNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["studentIds"]; ok && len(p["studentIds"].([]int64)) > 0 {
		qs = qs.Filter("student_id__in", p["studentIds"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if _, isLimit := p["limit"]; isLimit {
		qs = qs.Limit(p["limit"])
	} else if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&newNoticeList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, newNotice := range newNoticeList {
			seconds := time.Now().Unix() -  newNotice.Created.Unix()
			if seconds < 60 {
				newNotice.CreateShowIndex = fmt.Sprintf("%d秒前", seconds)
			} else if seconds < 3600 {
				minutes := seconds / 60
				newNotice.CreateShowIndex = fmt.Sprintf("%d分钟前", minutes)
			} else if seconds < 86400 {
				hours := seconds / 3600
				newNotice.CreateShowIndex = fmt.Sprintf("%d小时前", hours)
			} else {
				days := seconds / 86400
				newNotice.CreateShowIndex = fmt.Sprintf("%d天前", days)
			}
			newNotice.TypeShow = models.NewNoticeType[newNotice.Type]
		}
	}
	if _, ok := p["api"]; ok && p["api"].(bool) {
		for _, newNotice := range newNoticeList {
			seconds := time.Now().Unix() -  newNotice.Created.Unix()
			if seconds < 60 {
				newNotice.CreateShowIndex = fmt.Sprintf("%d秒前", seconds)
			} else if seconds < 3600 {
				minutes := seconds / 60
				newNotice.CreateShowIndex = fmt.Sprintf("%d分钟前", minutes)
			} else if seconds < 86400 {
				hours := seconds / 3600
				newNotice.CreateShowIndex = fmt.Sprintf("%d小时前", hours)
			} else {
				days := seconds / 86400
				newNotice.CreateShowIndex = fmt.Sprintf("%d天前", days)
			}
			newNotice.SchoolName = fmt.Sprintf("%s官方", newNotice.Student.School.Name)
			newNotice.StudentName = newNotice.Student.RealName
			newNotice.TypeShow = models.NewNoticeType[newNotice.Type]
			switch newNotice.Type {
			case 1:
				newNotice.MiniUrl = "/pages/access_control/access_control"
			case 2:
				newNotice.MiniUrl = "/pages/school_bus/school_bus"
			case 3:
				newNotice.MiniUrl = "/pages/homework/homework"
			case 4:
				newNotice.MiniUrl = "/pages/meeting/meeting"
			case 5:
				newNotice.MiniUrl = "/pages/grade/grade"
			case 6:
				newNotice.MiniUrl = "/pages/course/course"
			}
		}
	}
	return totalCount, newNoticeList
}